<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'

require('echarts/theme/royal') // echarts theme
import {debounce} from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    opinionSeriesData:{
      type: Array,
      default: []
    }
  },
  data() {
    return {
      chart: null,
      opinion: {
        legend: {
          orient: 'vertical',
          left: 'left',
        },
        toolbox: {
          show: true,
          feature: {
            mark: {show: true},
            dataView: {show: true, readOnly: false},
            restore: {show: true},
            saveAsImage: {show: true}
          }
        },
        series: [
          {
            name: '面积模式',
            type: 'pie',
            radius: [30, 120],
            center: ['50%', '50%'],
            roseType: 'area',
            itemStyle: {
              borderRadius: 8
            },
            data: this.opinionSeriesData
          }
        ]
      }
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    opinionSeriesData(newValue, oldValue) {
      console.log("oldValue", JSON.stringify(oldValue))
      console.log("newValue", JSON.stringify(newValue))
      this.opinion.series[0].data = newValue
      this.chart.setOption(this.opinion);
    },
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'royal')
      this.chart.setOption(this.opinion)

    }
  }
}
</script>
